package org.wuqinghua.chapter02;

import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieService;
import org.wuqinghua.BaseTest;
import org.wuqinghua.model.Car;
import org.wuqinghua.model.Person;

/**
 * Created by wuqinghua on 18/4/8.
 * <p>
 * 业务场景: 某品牌车(Car)进行打折处理。折扣规则如下：
 * - 年龄超过60岁，打八折
 * - 其它年龄打九折
 */
public class Drools7ApiTest extends BaseTest{

    @Test
    public void drools7ApiTest() {
//        // 1.需要在resources下含有一个META-INF/kmodule.xml
//
//        // 2.创建KieServices
//        KieServices kieServices = KieServices.get();
//
//        // 3.创建Container
//        KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
//
//
//        // 4.创建Session
//        KieSession kieSession = kieClasspathContainer.newKieSession("all-rules");
        KieSession kieSession = getKieSession("test-drools7");

        Person p1 = new Person();
        p1.setAge(30);
        Car c1 = new Car();
        c1.setPerson(p1);


        Person p2 = new Person();
        p2.setAge(70);
        Car c2 = new Car();
        c2.setPerson(p2);


        kieSession.insert(c1);
        kieSession.insert(c2);

        // 5.执行规则
        int count = kieSession.fireAllRules();
        kieSession.dispose();


        System.out.println("Fire " + count + " rule(s)!");
        System.out.println("c1 discount:" + c1.getDiscount() + "% c2 discount:" + c2.getDiscount
                () + "%");
    }
}
